package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.XcJqMapper;
import com.qf.dao.XcJqOrderDetailMapper;
import com.qf.dao.XcJqOrderMapper;
import com.qf.pojo.po.XcJq;
import com.qf.pojo.po.XcOrder;
import com.qf.pojo.po.XcOrderDetail;
import com.qf.pojo.po.XcUser;
import com.qf.pojo.req.JqOrderReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.resp.XcJqOrderResp;
import com.qf.service.JqOrderService;
import com.qf.utils.GenerateOrderNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 门票订单
 * @author WYD
 * @date 2021/10/26  9:52
 */
@Service
public class JqOrderServiceImpl implements JqOrderService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    XcJqOrderMapper xcJqOrderMapper;

    @Autowired
    XcJqOrderDetailMapper xcJqOrderDetailMapper;

    @Autowired
    XcJqMapper xcJqMapper;

    @Override
    public BaseResp addOrder(JqOrderReq jqOrderReq, HttpServletRequest httpServletRequest) {
        System.out.println("jqOrderReq"+jqOrderReq);
        //根据景区id查询数据库相应的票数
//        XcJq qcDetailByQcId = xcJqMapper.findQcDetailByQcId(jqOrderReq.getQcId());
//        Integer qcNum = qcDetailByQcId.getQcNum();
//        //比对票的个数
//        if ( qcNum < 1){
//            return new BaseResp(1,"票数不充足",null,null);
//        }

        //从redis中获取用户信息
        String token = httpServletRequest.getHeader("token");
        Object o = redisTemplate.opsForValue().get(token);
        //使用JSON转化，//判断该用户是否登录
        XcUser xcUser = JSONObject.parseObject(JSONObject.toJSONString(o), XcUser.class);
        if (xcUser==null){
            return new BaseResp(1,"该用户登录已过期，请重新登录！",null,null);
        }

////            获取新票数
//           int qcNum1 = qcNum - jqOrderReq.getQcNum();
////            订单生成成功，数据库数据票数减少
//            XcJq xcJq = qcDetailByQcId.get(0);
//            xcJq.setQcNum(qcNum1);
//            int i = xcJqMapper.updateQcNum(qcNum1 ,xcJq.getQcId());
//            if (i>0){
//                return new BaseResp(1,"更新成功",null,null);
//            }

        //生成总订单
        String generate = GenerateOrderNum.generate("");
        //创建Order属性
        XcOrder xcOrder = new XcOrder();
        xcOrder.setOrderId(generate); //订单号
        xcOrder.setOrderPay(jqOrderReq.getMoney()); // 订单
        xcOrder.setOrderStatus(1); // 订单状态
        xcOrder.setOrderType(3); //景区
        xcOrder.setOrderUserId(xcUser.getId()); //订单用户id   xcUser.getId()
        xcOrder.setCreateTime(new Date()); // 订单 时间
        //批量添加入数据库
        xcJqOrderMapper.insertOrder(xcOrder);

        //生成子订单
        String generate1 = GenerateOrderNum.generate("");
        XcOrderDetail xcOrderDetail = new XcOrderDetail();
        xcOrderDetail.setOrderDetailId(generate1);
        xcOrderDetail.setCreateTime(new Date());
        xcOrderDetail.setOrderId(xcOrder.getOrderId());
        xcOrderDetail.setJqId(jqOrderReq.getQcId());
        //批量添加数据库
        xcJqOrderDetailMapper.insertOrderDetail(xcOrderDetail);

        //订单成功减票
        //先查出当前景区的票数
        XcJq qcDetailByQcId = xcJqMapper.findQcDetailByQcId(jqOrderReq.getQcId());
        System.out.println(qcDetailByQcId);
        if (qcDetailByQcId.getQcNum()<jqOrderReq.getQcNum()){
            return new BaseResp(0,"票数不足",null,null);
        }
        int i = xcJqMapper.updateQcNum(qcDetailByQcId.getQcNum()-jqOrderReq.getQcNum() , jqOrderReq.getQcId());
//        redisTemplate.opsForValue ().set ("qcNum", jqOrderReq.getQcNum());
//        redisTemplate.opsForValue ().set ("qcId", jqOrderReq.getQcId());

        redisTemplate.opsForValue ().set (jqOrderReq.getQcId().toString(), qcDetailByQcId.getQcNum()-jqOrderReq.getQcNum());

        return  new BaseResp(0,"订单生成成功",null,null);
    }

    /**
     *  订单展示，查询订单
     * @param request
     * @return
     */
    @Override
    public BaseResp findXcOrderById(HttpServletRequest request) {
        //从redis中获取用户信息
        String token = request.getHeader("token");
        //判断该用户是否登录
        Object o = redisTemplate.opsForValue().get(token);
        //使用JSON转化
        XcUser xcUser = JSONObject.parseObject(JSONObject.toJSONString(o), XcUser.class);
        if (xcUser==null){
            return new BaseResp(1,"该用户登录已过期，请重新登录！",null,null);
        }

       List<XcJqOrderResp> list = xcJqOrderDetailMapper.findXcOrderById(xcUser.getId());
        System.out.println("景区order"+list);

        return new BaseResp(0,"订单查询成功",null,list);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @Override
    @Transactional
    public BaseResp cancelOrder(String orderId) {
       //查出当前门票订单信息
       XcOrderDetail xcByOrderId = xcJqOrderDetailMapper.findXcByOrderId(orderId);

        //删除订单
        int order1 = xcJqOrderMapper.deleteOrder(orderId);
        int order2 = xcJqOrderDetailMapper.deleteOrderDetail(orderId);

        if (order1 > 0 && order2 > 0){
            Integer qcNum = (Integer) redisTemplate.opsForValue().get(xcByOrderId.getJqId().toString());
            System.out.println("票数"+qcNum);
            System.out.println("景区id"+xcByOrderId.getJqId());
            int i = xcJqMapper.addOrderNum(qcNum+1, xcByOrderId.getJqId());
            return new BaseResp(0, "订单删除成功", null, null);
        }
        return new BaseResp(0, "请勿重复操作", null, null);
    }

    @Override
    public BaseResp findXcOrderByIdAll(HttpServletRequest request) {
        //从redis中获取用户信息
        String token = request.getHeader("token");
        //判断该用户是否登录
        Object o = redisTemplate.opsForValue().get(token);
        //使用JSON转化
        XcUser xcUser = JSONObject.parseObject(JSONObject.toJSONString(o), XcUser.class);
        if (xcUser==null){
            return new BaseResp(1,"该用户登录已过期，请重新登录！",null,null);
        }

        List<XcJqOrderResp> list = xcJqOrderDetailMapper.findXcOrderByIdAll(xcUser.getId());
        System.out.println("景区order"+list);

        return new BaseResp(0,"订单查询成功",null,list);
    }

}
